/**
 * Created by Administrator on 2016/8/17.
 */
class CommonUtils {
    /**
  * 给字体添加描边
  * @param lable      文字
  * @param color      表示文本的描边颜色
  * @param width      描边宽度。
  */
    public addLableStrokeColor(lable: eui.Label, color: any, width: any): void {
        lable.strokeColor = color;
        lable.stroke = width;
    }

    /**
     * 深度复制
     * @param _data
     */
    public copyDataHandler(obj: any): any {
        var newObj;
        if (obj instanceof Array) {
            newObj = [];
        }
        else if (obj instanceof Object) {
            newObj = {};
        }
        else {
            return obj;
        }
        var keys = Object.keys(obj);
        for (var i: number = 0, len = keys.length; i < len; i++) {
            var key = keys[i];
            newObj[key] = this.copyDataHandler(obj[key]);
        }
        return newObj;
    }

    /**
     * 锁屏
     */
    public lock(): void {
        StageUtils.getIt().getStage().touchEnabled = StageUtils.getIt().getStage().touchChildren = false;
    }

    /**
     * 解屏
     */
    public unlock(): void {
        StageUtils.getIt().getStage().touchEnabled = StageUtils.getIt().getStage().touchChildren = true;
    }

    /**
     * int64转number
     * @param obj
     * @returns {number}
     */
    public int64ToNumber(obj) {
        return parseInt(obj.toString());
    }

    /** 
     *  副本提示信息专用
     */
    public copyInfoNumOverLength(num: number): string {
        var str = null;
        if (num < 100000) {
            str = "" + num;
        } else {
            str = (Math.floor(num / 10000).toString()) + `w`;//万
        }

        return str;
    }

    //获取随机值
    public getRandomNum(min, max): number {
        var Range = max - min;
        var Rand = Math.random();
        return Math.floor(min + Math.round(Rand * Range));
    }

    //旁边固定距离随机值
    public getRandomSidePoint(posX: number, posY: number, min: number, max: number): egret.Point {
        var point = new egret.Point();
        var flagX = Math.random() > 0.5 ? 1 : -1;
        var flagY = Math.random() > 0.5 ? 1 : -1;
        point.x = posX + flagX * this.getRandomNum(min, max);
        point.y = posY + flagY * this.getRandomNum(min, max);
        return point;
    }

    //两点之间距离
    public getDistance(x1: number, y1: number, x2: number, y2: number): number {
        return Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
    }

    //从大到小
    public maxToMin(list: Array<any>): void {
        var len = list.length, tmp;
        for (var i = 0; i < len; i++) {
            for (var j = len - 1; j > i; j--) {
                var aData = list[j];
                var bData = list[j - 1];

                if (aData > bData) {
                    tmp = list[j - 1];
                    list[j - 1] = list[j];
                    list[j] = tmp;
                }
            }
        }
    }

    //从小到大
    public minToMax(list: Array<any>): void {
        var len = list.length, tmp;
        for (var i = 0; i < len; i++) {
            for (var j = len - 1; j > i; j--) {
                var aData = list[j];
                var bData = list[j - 1];

                if (aData < bData) {
                    tmp = list[j - 1];
                    list[j - 1] = list[j];
                    list[j] = tmp;
                }
            }
        }
    }

    private reverse(arr: Array<any>, b: number, e: number) {
        for (; b < e; b++ , e--) {
            var temp = arr[e];
            arr[e] = arr[b];
            arr[b] = temp;
        }
    }
    /**
     *数组值按顺序向右移动距离
     * arr 要移动数组
     * N 数组长度
     * k 移动距离
     */
    public rightShift(arr: Array<any>, N: number, k: number) {
        var k: number = k % N;
        this.reverse(arr, 0, N - k - 1);
        this.reverse(arr, N - k, N - 1);
        this.reverse(arr, 0, N - 1);
    }

    //随机字符串
    public randomString(len) {
        len = len || 32;
        var $chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678';    /****默认去掉了容易混淆的字符oOLl,9gq,Vv,Uu,I1****/
        var maxPos = $chars.length;
        var pwd = '';
        for (var i = 0; i < len; i++) {
            pwd += $chars.charAt(Math.floor(Math.random() * maxPos));
        }
        return pwd;
    }
	
    /**
     *  对象转数组
     */
    public ObjectToArr(obj: any): any[] {
        let tempArr = [];
        for (let key in obj) {
            tempArr.push(obj[key]);
        }

        return tempArr;
    }

    /**
	 * 值装换
	 * 200000012   表示值长度2，值为12
	 */
    public getChangeValue(v: number): string {
        let num = Math.floor(v / 100000000);
        let value = Math.floor(v % 100000000) + "";
        if (num >= 9 && value.length < 9) {
            let needLen = num - value.length;
            for (let i = 0; i < needLen; i++) {
                value += "0";
            }
        }
        return value;
    }

    private static _instance: CommonUtils;
    public static getIt(): CommonUtils {
        if (!this._instance)
            this._instance = new CommonUtils();

        return this._instance;
    }
}
